package com.zygvip.demand.service.impl;

import com.zygvip.demand.dao.AppRequireDao;
import com.zygvip.demand.entity.AppRequire;
import com.zygvip.demand.service.AppIndustryService;
import com.zygvip.demand.service.AppRequireService;
import com.zygvip.demand.vo.ContactVo;
import com.zygvip.demand.vo.RequireVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description
 *
 * @author 马乌提
 * @create 2018-02-02 10:43
 */
@Service
public class AppRequireServiceImpl implements AppRequireService{

    @Autowired
    private AppRequireDao appRequireDao;

    @Override
    public AppRequire addRequire(RequireVo requireVo) {
        AppRequire require = new AppRequire();
        require.setContent(requireVo.getContent());
        require.setIndustryId(requireVo.getIndustryId());
        require.setGmtCreated(new Timestamp(System.currentTimeMillis()));
        Byte isSend = 0;
        Byte hasContact = 0;
        require.setIsSend(isSend);
        require.setHasContact(hasContact);
        require = appRequireDao.save(require);
        return require;
    }

    @Override
    public AppRequire bindContact(ContactVo contactVo) {
        Integer requireId = contactVo.getRequireId();
        AppRequire require = appRequireDao.findOne(requireId);
        Byte hasContact = 1;
        if(hasContact.equals(require.getHasContact())){
            return require;
        }
        require.setEmail(contactVo.getEmail());
        require.setRealname(contactVo.getRealname());
        require.setMobile(contactVo.getMobile());
        require.setGmtModified(new Timestamp(System.currentTimeMillis()));
        require.setHasContact(hasContact);
        require = appRequireDao.save(require);
        return require;
    }

    @Override
    public List<AppRequire> unsentRequires() {
        Specification specification = new Specification<AppRequire>() {
            @Override
            public Predicate toPredicate(Root<AppRequire> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                return cb.equal(root.get("isSend"),0);
            }
        };
        Sort.Direction direction = Sort.Direction.DESC;
        Sort sort = new Sort(direction,"gmtCreated");
        List<AppRequire> list = appRequireDao.findAll(specification,sort);
        return list;
    }

    @Override
    public AppRequire markSend(AppRequire appRequire) {
        Byte isSend = 1;
        appRequire.setIsSend(isSend);
        appRequire.setSendTime(new Timestamp(System.currentTimeMillis()));
        AppRequire require = appRequireDao.save(appRequire);
        return require;
    }
}
